package dsproject;

import java.io.Serializable;
import java.util.HashSet;

/**
 *  This class is used to simulate the message sent through the network.
 *  The message is received by the noode, which operate its RS caching protocol
 */
public class Message implements Serializable {

    private String header;// The header is a simple container not defined yet, which should have communication protocol data
    private HashSet<ResourceState> body; // The purpose/object/reason of the message
    private HashSet<ResourceState> content;//The piggybacked set of information
    private int seqId;//the message sequence id
    private final Request request;//the the request of the message
    private final TYPE type;

    public Message(int seqId, Request request, HashSet body, HashSet content, TYPE mtype) {
        this.header = "";
        this.body = body;
        this.content = content;
        this.seqId = seqId;
        this.request = request;
        this.type = mtype;
    }

    public void incSeqId() {
        this.seqId++;
    }

    public enum TYPE{
        PERIODIC, NORMAL
    }

    public int getSize() {

        int size = 0;
        //SeqId
        size += Integer.SIZE;
        //ReqId
        size += Integer.SIZE;
        //header
        size += 192;//this.header.getBytes().length;
        //body
        for(ResourceState rs: this.body){
            size += rs.getSize();
        }
        //content
        for(ResourceState rs: this.content){
            size += rs.getSize();
        }
        
        return size;
    }

    public HashSet<ResourceState> getBody() {
        return body;
    }

    public void setBody(HashSet body) {
        this.body = body;
    }

    public HashSet<ResourceState> getContent() {
        return content;
    }

    public Request getRequest() {
        return request;
    }

    public int getSeqId() {
        return seqId;
    }

    public void setContent(HashSet content) {
        this.content = content;
    }

    public Node getNext() {
        return request.getNext();
    }

    public TYPE getType() {
        return type;
    }

}
